import AntModal from "@/components/pt/dialog/AntModal";
import {
  getSysPublishLog,
  addSysPublishLog,
  updateSysPublishLog,
  findMaxSort,
} from "@/api/biz/sysPublishLog";
import Vditor from "vditor";
import "vditor/dist/index.css";
export default {
  name: "CreateForm",
  props: {},
  components: {
    AntModal,
    Vditor,
  },
  data() {
    return {
      open: false,
      spinning: false,
      delayTime: 100,
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      loading: false,
      total: 0,
      id: undefined,
      formTitle: "添加发布日志",
      // 表单参数
      form: {},
      rules: {},
      contentEditorEdit: "",
      contentText: "",
      contentTextHtml: "",
      vditorToolbar: [
        // 将上传图片和录音按钮隐藏
        "emoji",
        "headings",
        "bold",
        "italic",
        "strike",
        "link",
        "|",
        "list",
        "ordered-list",
        "check",
        "outdent",
        "indent",
        "|",
        "quote",
        "line",
        "code",
        "inline-code",
        "insert-before",
        "insert-after",
        "|",
        "table",
        "|",
        "undo",
        "redo",
        "|",
        "fullscreen",
        "edit-mode",
        {
          name: "more",
          toolbar: [
            "both",
            "code-theme",
            "content-theme",
            "export",
            "outline",
            "preview",
            "devtools",
          ],
        },
      ],
    };
  },
  filters: {},
  created() {},
  computed: {},
  watch: {},
  mounted() {},
  methods: {
    onClose() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$emit("close");
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        versionNumber: undefined,

        info: undefined,
      };
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      /** 获取最大编号 */
      findMaxSort().then((response) => {
        this.form.sort = response.data;
        this.formTitle = "添加发布日志";
      });
      this.$nextTick(() => {
        if (this.contentEditorEdit === "") {
          this.contentEditorEdit = new Vditor("vditor", {
            height: 180,
            toolbarConfig: {
              pin: true,
            },
            toolbar: this.vditorToolbar,
            cache: {
              enable: false,
            },
            after: () => {
              this.contentEditorEdit.setValue(this.contentText);
            },
          });
        } else {
          this.contentEditorEdit.setValue(this.contentText);
        }
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.spinning = !this.spinning;
      const sysPublishLogId = row.id;
      getSysPublishLog(sysPublishLogId).then((response) => {
        this.form = response.data;
        this.formTitle = "修改发布日志";
        this.spinning = !this.spinning;
        this.$nextTick(() => {
          if (this.contentEditorEdit === "") {
            this.contentEditorEdit = new Vditor("vditorEdit", {
              height: 360,
              toolbarConfig: {
                pin: true,
              },
              toolbar: this.vditorToolbar,
              cache: {
                enable: false,
              },
              after: () => {
                this.contentEditorEdit.setValue(response.data.info);
              },
            });
          } else {
            this.contentEditorEdit.setValue(response.data.info);
          }
        });
      });
    },
    /** 提交按钮 */
    submitForm: function () {
      this.form.info = this.contentEditorEdit.getValue();
      this.form.infoHtml = this.contentEditorEdit.getHTML();
      this.$refs.form.validate((valid) => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form));
          if (this.form.id !== undefined) {
            updateSysPublishLog(saveForm).then((response) => {
              this.$message.success("修改成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          } else {
            addSysPublishLog(saveForm).then((response) => {
              this.$message.success("新增成功", 3);
              this.open = false;
              this.$emit("ok");
              this.$emit("close");
            });
          }
        } else {
          return false;
        }
      });
    },
    back() {
      const index = "/biz/publishlog/index";
      this.$router.push(index);
    },
  },
};
